﻿using DevExpress.XtraEditors;
using DXApplicationTangche.UC.圣同润.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.圣同润
{
    public partial class Form货品规格详情 : DevExpress.XtraEditors.XtraForm
    {
        private String goodsNo { get; set; }
        public List<GoodsDetailDTO> goodsDetails = new List<GoodsDetailDTO>();
        public Form货品规格详情(String goodsNo,List<GoodsDetailDTO> goodsDetails)
        {
            InitializeComponent();
            this.goodsNo = goodsNo;
            this.goodsDetails = goodsDetails;
        }

        private void simpleButton保存_Click(object sender, EventArgs e)
        {
            String specNo = this.textBox商家编码.Text.Trim();
            String specName = this.textBox规格名称.Text.Trim();
            Double lowestPrice;
            try{ lowestPrice = Convert.ToDouble(this.textBox最低价.Text.Trim()); }catch{ lowestPrice = 0.0; };
            Double retailPrice;
            try { retailPrice = Convert.ToDouble(this.textBox零售价.Text.Trim()); } catch { retailPrice = 0.0; }
            Double wholesalePrice;
            try { wholesalePrice = Convert.ToDouble(this.textBox批发价.Text.Trim()); } catch { wholesalePrice = 0.0; }
            Double memberPrice;
            try { memberPrice = Convert.ToDouble(this.textBox会员价.Text.Trim()); } catch { memberPrice = 0.0; }
            Double marketPrice;
            try { marketPrice = Convert.ToDouble(this.textBox市场价.Text.Trim()); } catch { marketPrice = 0.0; }
            Boolean isLoweCost = "是".Equals(this.comboBox允许低于成本.Text.Trim())?true:false;
            String imgUrl = this.textBox图片链接.Text.Trim();
            String remark = this.textBox单品备注.Text.Trim();
            String taxCode = this.textBox税务编码.Text.Trim();
            //  商家编码不能为空
            if (String.IsNullOrEmpty(specNo))
            {
                MessageBox.Show("商家编码不能为空");
                return;
            }
            GoodsDetailDTO goodsDetail = 
                new GoodsDetailDTO(
                        specNo,
                        specName,
                        Convert.ToDouble(lowestPrice),
                        Convert.ToDouble(retailPrice),
                        Convert.ToDouble(wholesalePrice),
                        Convert.ToDouble(memberPrice),
                        Convert.ToDouble(marketPrice),
                        isLoweCost,
                        imgUrl,
                        remark,
                        taxCode
                    );
            //  判断商家编码是否重复
            if (!this.goodsDetails.Exists(dto => dto.specNo.Equals(goodsDetail.specNo)))
            {
                Boolean re = 货品Service.saveProductGoodsDetail(goodsNo, goodsDetail);
                if (re)
                {
                    goodsDetails.Add(goodsDetail);
                }
                else
                {
                    MessageBox.Show("保存失败");
                    return;
                }
                this.Close();
            }
            else {
                MessageBox.Show("商家编码已存在");
                return;
            }
        }
    }
}